<?php

/**
 * @Author: Wang chunsheng  email:2192138785@qq.com
 * @Date:   2023-05-06 19:34:21
 * @Last Modified by:   Wang chunsheng  email:2192138785@qq.com
 * @Last Modified time: 2023-06-07 18:29:59
 */

namespace App\Api;

use Common\ApiController;
use Common\Helpers\DebugHelper;
use Common\Models\Enums\DeviceTypeEnums;
use Common\Services\OperationLog;
use Common\Services\Sys\DeviceService;
use Common\Services\Sys\ProjectService;
use Hyperf\Constants\Exception\ConstantsException;
use Hyperf\HttpServer\Annotation\AutoController;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

#[AutoController(prefix: '{project_sn}/device')]
class DeviceController extends ApiController
{
    public array $signOptional = ['types','lockBack'];

    /**
     * 蓝牙设备检索服务
     * @return array
     * @throws ConstantsException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function search():array
    {
        $request = $this->request;
        $mac =  $request->input('mac');
        $device_type =  $request->input('device_type');
        $name =  $request->input('name');
        $project_id = $this->getAttribute('project_id');
        DebugHelper::consoleWrite('mac检索',[
            'mac' =>$mac
        ]);
        $Res = DeviceService::searchDevice($project_id,$device_type,$name,$mac);
        DebugHelper::consoleWrite('设备检索结果',[
            'res'=>$Res
        ]);
        if ($Res){
            return $this->writeJson(200, $Res, '设备存在');
        }else{
            return $this->writeJson(405, [], '设备不存在');
        }
    }

    /**
     * 操作日志
     * @return array
     * @date 2023-06-06
     * @example
     * @author Wang Chunsheng
     * @since
     */
    public function log(): array
    {
        $request = $this->request;
        $Param = $request->all();
        $page = $Param['page'] ?? 1;
        $pageSize = $Param['pageSize'] ?? 10;
        $member = $request->getAttribute('member');
        $project_id = $request->getAttribute('project_id');
        $device_id = $Param['device_id'] ?? 10;
        DebugHelper::consoleWrite('挂载数据',[
            'member' => $member,
            'project_id'=> $project_id
        ]);

        $Res = OperationLog::getLog($page, $pageSize, $project_id, $device_id);

        return $this->writeJson(200, $Res, '获取列表成功');
    }

    public function lockBack():array
    {
        $request = $this->request;
        $Param = $request->all();
        DebugHelper::consoleWrite('回调数据',$Param);
        return $this->writeJson(200, $Param, '回调数据');
    }

    /**
     * 修改全局设备信息
     * @return array
     */
    public function editSys():array
    {
        $request = $this->request;
        $Param = $request->all();
        DebugHelper::consoleWrite('回调数据',$Param);
        $id = $Param['id'];
        $title = $Param['title'];
        $cate_pid =  $Param['cate_pid']??0;
        $cate_id  = $Param['cate_id']??0;
        $desc  = $Param['desc']??'';
        $model_name  = $Param['model_name']??'';
        $device_sn = $Param['device_sn']??'';

        $Res = DeviceService::editDevice($id, $title,$cate_pid,$cate_id,$desc,$model_name,$device_sn);
        return $this->writeJson(200, $Res, '设置成功');
    }

    public function detailDevice():array
    {
        $request = $this->request;
        $Param = $request->all();
        
        $building_remark = $Param['building_remark'];
        $floor_remark = $Param['floor_remark'];
        $unit_remark = $Param['unit_remark'];
        $room_remark = $Param['room_remark'];
        $device_type = $Param['device_type'];

        $Res = DeviceService::detailDevice($device_type,$building_remark,$room_remark,$floor_remark,$unit_remark);

        return $this->writeJson(200, $Res, '获取成功');
    }


    /**
     * 项目详情
     * @return array
     * @date 2023-06-06
     * @example
     * @author Wang Chunsheng
     * @since
     */
    public function detailProject(): array
    {
        $request = $this->request;
        $id = $request->input('id');
        $Res = ProjectService::detailProject($id);

        return $this->writeJson(200, $Res, '获取成功');
    }

    /**
     * 设备类型
     * @return array
     * @date 2023-06-06
     * @example
     * @author Wang Chunsheng
     * @since
     */
    public function types(): array
    {
        $list = DeviceTypeEnums::$list;
        return $this->writeJson(200, $list, '获取成功');
    }
}
